<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="main_crumbs boxdq">
          <div class="main_crumbs_l">
            <a href="javascript:;" @click="$router.back(-1)">
              <i class="el-icon-arrow-down"></i>返回
            </a>
            <em>|</em>
            <span>调整损耗金额</span>
          </div>
          <el-button type="primary" @click="exportform">导出</el-button>
        </div>
        <div class="mainor_form" style="margin-top: 20px">
          <div class="mainor_te"></div>
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="70px"
          >
            <el-form-item label="日期" prop="establishTime">
              <el-date-picker
                v-model="establishTime"
                type="daterange"
                class="time220"
                :editable="false"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
              />
            </el-form-item>
            <el-form-item label="名称" prop="goods_spu_id">
              <change-goods-input
                ref="changeGoodsInput"
                @clear="
                  () => {
                    this.form.goods_spu_id = '';
                  }
                "
                @sendValue="
                  (data) => {
                    this.form.goods_spu_id = data.id;
                  }
                "
              ></change-goods-input>
            </el-form-item>
            <el-form-item label="分类" prop="category_id" class="wid220">
              <el-cascader
                v-model="form.category_id"
                :options="options"
                :props="defaultProps"
                ref="cascaderHandle"
              ></el-cascader>
            </el-form-item>
            <el-form-item>
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="reset"
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_table marginTop10" style="min-height: 550px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            :default-sort="{ prop: 'ullage_money', order: 'descending' }"
            :header-cell-class-name="this.$verticaline"
          >
            <el-table-column label="商品名称" prop="goods_name">
              <template slot-scope="scope">{{ scope.row.goods_name }}</template>
            </el-table-column>

            <el-table-column label="商品分类" prop="category_name" />
            <el-table-column label="SKU编码" prop="goods_spu_code" />
            <el-table-column label="单位" prop="unit_name" />

            <el-table-column
              label="损耗数量"
              sortable
              prop="ullage_num"
              :sort-method="
                (obj1, obj2) => sortByDate(obj1, obj2, 'ullage_num')
              "
            >
            </el-table-column>
            <el-table-column
              label="损耗金额"
              sortable
              prop="ullage_money"
              :sort-method="
                (obj1, obj2) => sortByDate(obj1, obj2, 'ullage_money')
              "
            >
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <el-pagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  get_loss_list,
  export_report_purchase_goods,
} from "@/api/report/index";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import dateOption from "@/options/dateOption";
import { parseTime, exportMethod } from "@/utils/index";
import { recursion, category } from "@/utils/index";

export default {
  components: {},
  data() {
    return {
      // 时间
      parseTime,
      exportMethod,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: [],

      listLoading: false,
      // 搜索
      form: {
        type: "3",
        start_time: "",
        end_time: "",
        pid: "",
        category_id: "",
        goods_spu_id: "",
      },
      establishTime: [],
      // 分类
      options: [],
      defaultProps: {
        children: "selfNext",
        label: "title",
        value: "id",
        checkStrictly: true,
      },
    };
  },
  created() {
    // 列表
    if (this.$route.params.date) {
      this.establishTime = this.$route.params.date;
      this.get_loss_list();
    } else {
      this.get_loss_list();
      // this.$router.back(-1);
    }
  },
  methods: {
    get_loss_list() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      get_loss_list(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = Object.values(res.data.data);
          this.totalCount = res.data.count;
          this.options = recursion(res.data.categoryList, true);
        }
      });
    },

    // 重置
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.$refs["changeGoodsInput"].name = "";
      this.establishTime = this.$route.params.date;
      this.getArticleList();
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.get_loss_list();
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.get_loss_list();
    },
    changePage(page) {
      this.current.page = page;
      this.get_loss_list();
    },

    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.start_time = this.establishTime[0] ? this.establishTime[0] : "";
      form.end_time = this.establishTime[1] ? this.establishTime[1] : "";
      form.pid = form.category_id[0] ? form.category_id[0] : "";
      form.category_id = category(form.category_id);
      return form;
    },
    // 排序
    sortByDate(obj1, obj2, type) {
      let val1 = obj1[type];
      let val2 = obj2[type];
      return val1 - val2;
    },
    // 导出
    exportform() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      export_report_purchase_goods(data).then((res) => {
        this.exportMethod(res, "调整损耗");
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 180px);
  }
}

// 支付状态
.success {
  color: $green;
}
.unpaid {
  color: $colour_red;
}
// 订单状态
.order0 {
  color: $order_color1;
}
.order1 {
  color: $order_color2;
}
.order2 {
  color: $order_color3;
}
.order4 {
  color: $order_color4;
}
</style>
